package com.lapost.client;

import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URLDecoder;

import org.apache.http.entity.StringEntity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class HttpHelper {

	public static final String TAG = HttpHelper.class.getSimpleName();
	
	
	/** Requesting data from AppEngine server */
	public static String doGet(String path) {
		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params, "UTF-8");
		params.setBooleanParameter("http.protocol.expect-continue", false);
				
		String url = Constants.SERVER_ADDRESS + path;
		Log.i(TAG, "GET "+url);
		String str = null;
	    HttpURLConnection connection = null; 
		try {
			HttpClient client = new DefaultHttpClient(params);
            HttpGet request = new HttpGet(new URI(url)); // uri= "http://w3mentor.com/"            
            HttpResponse response = client.execute(request);   
	        HttpEntity entity = response.getEntity();
	        String jsonText = EntityUtils.toString(entity, HTTP.UTF_8);
	        
	        str = URLDecoder.decode(jsonText, "UTF-8");
	        
		}catch(Exception e) {
			e.printStackTrace();
			
		}finally {
			if(connection != null) {
		        connection.disconnect(); 
		      }
		}
		return str;
	}
	
	/** Posting data to AppEngine server */
	public static void doPost(String path, String data) {
		String url = Constants.SERVER_ADDRESS + path;
		Log.i(TAG, "POST "+url);
	    try {
	    	// Create a new HttpClient and Post Header
		    HttpClient client = new DefaultHttpClient();
		    HttpPost request = new HttpPost(new URI(url));
		    // All your data
	        StringEntity entity = new StringEntity(data, HTTP.UTF_8);	       
	        
	        request.setEntity(entity);
	        // Execute HTTP Post Request
	        HttpResponse response = client.execute(request);
	        HttpEntity resEntity = response.getEntity();  
            if (resEntity != null) {    
                Log.i("POST RESPONSE",EntityUtils.toString(resEntity, HTTP.UTF_8));
            }
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
	/** Posting data to AppEngine server */
	public static void doPut(String path, String data) {
		String url = Constants.SERVER_ADDRESS + path;
		Log.i(TAG, "PUT "+url);
	    try {
	    	// Create a new HttpClient and Post Header
		    HttpClient client = new DefaultHttpClient();
		    HttpPut request = new HttpPut(new URI(url));
		    //request.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
		    //request.addHeader("User-Agent", "Android");
		    // All your data
	        StringEntity entity = new StringEntity(data, HTTP.UTF_8);
	        request.setEntity(entity);
	        // Execute HTTP Post Request
	        HttpResponse response = client.execute(request);
	        HttpEntity resEntity = response.getEntity();  
            if (resEntity != null) {    
                Log.i("PUT RESPONSE",EntityUtils.toString(resEntity, HTTP.UTF_8));
            }
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}
}
